RIGHT JOIN
In this lesson, we will discuss the RIGHT JOIN keyword.
We'll cover the following
RIGHT JOIN#
The RIGHT JOIN keyword returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL
from the left side when there is no match.
Syntax#
SELECT table1.column1, table2.column2...
FROM table1
RIGHT JOIN table2
ON table1.common_field = table2.common_field;
Note: In some databases, RIGHT JOIN is called RIGHT OUTER JOIN.
Example#
Let’s say we want to return all orders and any customers that have placed an order:
The SQL query to retrieve all orders and some of the customers(those who have placed an order):
As you can see, the RIGHT JOIN keyword returns all records from the right table (ORDERS), even if there are no matches in the left table (CUSTOMERS).
Quick quiz!#
Will the following query return the NAME and ADDRESS of the customer that ordered an item along with the items’ ORDER_ID?
SELECT CUSTOMERS.NAME, CUSTOMERS.ADDRESS ,ORDERS.ORDER_ID
FROM CUSTOMERS
RIGHT JOIN ORDERS
ON ID = CUSTOMER_ID;
A)
True
B)
False